package com.wanhu.player
{
	import flash.events.*;
	import flash.media.*;
	import flash.net.NetStream;


	public class CanvasWrapper extends VideoCanvas
	{
		public var yVideo:Video=new Video(160, 120);
		public var partnerVideo:Video=new Video(640, 480);

		public function CanvasWrapper()
		{
			super();
		}

		public function initVideo():void
		{
			var cam:Camera=null;
			var camIndex:int=Supervisor.js.getVar(Constants.CAMERA_INDEX);
			if (Camera.names && Camera.names.length > camIndex)
			{
				cam=Camera.getCamera("" + camIndex);
			}
			var mic:Microphone=null;
			var micIndex:int=Supervisor.js.getVar(Constants.MIC_INDEX);
			if (Microphone.names && Microphone.names.length > micIndex)
			{
				mic=Microphone.getMicrophone(micIndex);
			}
			if (mic)
			{
				Supervisor.mic=mic;
				mic.codec=SoundCodec.SPEEX;
				mic.setSilenceLevel(5, 1000);
				mic.framesPerPacket=1;
				mic.setUseEchoSuppression(true);
				mic.gain=100;

				if (Supervisor.sendStream)
				{
					Supervisor.sendStream.attachAudio(mic);
				}

				if (Supervisor.outgoingStream)
				{
					Supervisor.outgoingStream.attachAudio(mic);
				}
			}
			if (cam)
			{
				Supervisor.cam=cam;
				cam.setMode(640, 480, 15);
				cam.setQuality(0, 50);
				Supervisor.sendVideo=String(Supervisor.js.getVar(Constants.SEND_VIDEO_CHECK_BOX)) == "true";
				Debug.print("SendVideoBox: " + String(Supervisor.js.getVar(Constants.SEND_VIDEO_CHECK_BOX)) + "  equal:" + Supervisor.sendVideo);
				if (Supervisor.sendVideo)
				{
					this.yVideo.attachCamera(cam);
				}
				else
				{
					this.yVideo.attachCamera(null);
				}

				if (!localVideo.contains(yVideo))
				{
					localVideo.addChild(yVideo);
				}

				if (Supervisor.sendStream)
				{
					if (Supervisor.sendVideo)
					{
						Supervisor.sendStream.attachCamera(cam);
					}
					else
					{
						Supervisor.sendStream.attachCamera(null);
					}
				}

				if (Supervisor.outgoingStream)
				{
					if (Supervisor.sendVideo)
					{
						Supervisor.outgoingStream.attachCamera(cam);
					}
					else
					{
						Supervisor.outgoingStream.attachCamera(null);
					}
				}
				/*
				this.partnerVideo.attachCamera(cam);

				if (!remoteVideo.contains(partnerVideo))
				{
					remoteVideo.addChild(partnerVideo);
				}*/
			}
		}

		public function attach(ns:NetStream=null):void
		{
			if (ns == null)
			{
				if (this.remoteVideo.contains(this.partnerVideo))
				{
					this.remoteVideo.removeChild(this.partnerVideo);
				}
				this.partnerVideo.clear();
			}
			else
			{
				this.partnerVideo.clear();
				this.partnerVideo.attachNetStream(ns);
				if (!this.remoteVideo.contains(this.partnerVideo))
				{
					this.remoteVideo.addChild(this.partnerVideo);
				}
			}
		}
	}
}
